* ==============================================================================
* 	Measuring Knowledge of Parties' Legislative Seat Shares
* 	Lee, Haime & Stevenson 
* 	Replication Do File: Tables and Figures
* ==============================================================================
set scheme sol /* ado file available at: https://dl.dropbox.com/u/3011470/scheme_sol.zip */
set more off

********************************************************************************
*****					 *******************************************************
*****   UNITED KINGDOM   *******************************************************
*****					 *******************************************************
********************************************************************************

use "datasetUK.dta", clear


* =====================
* Table 1 UK
* =====================	

	/** [Row 1] Gave a 100% to one party (numzeros==11) **/	
		tab numzeros
			
	/** [Row 2] Answered the question in under 10s **/
		gen under10s=(time<=10) if time!=.
		tab under10s
		
	/** [Row 3] Answered the question in under 20s **/
		gen under20s=(time<=20) if time!=.
		tab under20s

	/** [Rows 4] A or B **/
		gen aorb = 0
		replace aorb = 1 if numzeros==11 | under10s==1
		tab aorb
			
	/** [Rows 5] A or C **/
		gen aorc = 0
		replace aorc = 1 if numzeros==11 | under20s==1
		tab aorc
					
* =====================
* Table 2 UK
* =====================
	
	/** [Row 1] Abandoned **/	
	preserve
		* load dataset with all individuals who started the survey
		use "hazardUK.dta", clear
			gen group=.	
			replace group = 1 if seatsfixed_t !=.
			replace group = 2 if seatsrandom_t !=.
			replace group = 3 if percentfixed_t !=.
			replace group = 4 if percentrandom_t !=.
			label def group 1 "seats fixed" 2 "seats random" 3 "percent fixed" 4 "percent random" 
			label val group group
		* denominators
			tabstat seatsfixed_tc seatsrandom_tc percentfixed_tc percentrandom_tc , stats(n)
			gen reached = 1 if seatsfixed_tc!=.| seatsrandom_tc!=.| percentfixed_tc!=.| percentrandom_tc!=.
		* numerators: count the abandoned, by looking at missing answers for a set of questions coming after the seats question
			gen dropout=1 if reached==1
				set more off
				foreach i of varlist taxspending_set1-scottishindep_set5 {
					replace dropout=0 if `i'!=.&dropout==1
				}
		* abandoned (count and percentage)
			tab dropout group, col
	restore
	
	/** [Row 2] Gave a 100% to one party (numzeros==11) ***/
		tab numzeros group 
	
	/** [Row 3] Under 10s (under10s==1) **/
		tab under10s group, col

* =====================
* Figure 3 
* =====================	
	  
	/** Conservative **/ 
		histogram sizepercent_lab, xline(33, lpattern(solid)) xline(41, lpattern(dash)) ///
		freq by(group, title("Conservative") note("")) xtitle("Percent of Seats Assigned", size(small))  name(con, replace)
	
	/** Labour **/ 
	  	histogram sizepercent_cons, xline(34, lpattern(solid)) xline(43, lpattern(dash)) ///
		freq by(group, title("Labour") note("")) xtitle("Percent of Seats Assigned", size(small)) ytitle ("") name(lab, replace)
	  
		graph combine con lab, xsize(8) ysize(3) scale(1.4)
		graph export "figure3.pdf", replace
		
* =====================
* Footnote 13: UK 
* =====================	
	
	/** calculate mean absolute error **/
	  
	  capture drop abserrorvote_con
	  gen abserrorvote_con=.
	  replace abserrorvote_con=abs(sizepercent_cons-34)
	  tabstat abserrorvote_con, by(group) stats(mean median n)
	  
	  capture drop abserrorseat_con
	  gen abserrorseat_con=.
	  replace abserrorseat_con=abs(sizepercent_cons-43)	  
	  tabstat abserrorseat_con, by(group) stats(mean median n)
	  
	  capture drop abserrorvote_lab
	  gen abserrorvote_lab=.
	  replace abserrorvote_lab=abs(sizepercent_lab-33)
	  tabstat abserrorvote_lab, by(group) stats(mean median n)
	  
	  capture drop abserrorseat_con
	  gen abserrorseat_con=.
	  replace abserrorseat_con=abs(sizepercent_lab-41)	  
	  tabstat abserrorseat_con, by(group) stats(mean median n)

* =====================
* Figure 4 UK 
* =====================	

	ssc install triplot

	gen cons=sizepercent_cons
	gen lab=sizepercent_lab
	egen other1=rowtotal(sizepercent_*)
	gen other=other1-(cons+lab)
	replace other=100 if other>100
	
	local name1 "Seat, Numbers"
	local name2 "Seat, Shares"

	foreach jj of numlist 1 2 {
	triplot cons lab  other if group==`jj', y mcolor(gray) ltext(Cons) rtext(Lab) btext(Other) ///
		 max(100) title("`name`jj''", size(medsmall)) name(gg`jj', replace) nodraw
		}

	graph combine gg1 gg2, rows(1) imargin(tiny) xsize(10) ysize(4) scale(1.6)	
	graph export "figure4.pdf", replace
			 
* =====================
* APPENDIX Figure C: UK 
* =====================	

	graph box clicks, over(numzeros) ytitle("Number of Clicks") ///
	b1title("Number of Zeros Reported")	title("United Kingdom") name(figureC_uk, replace)
	
	tabstat clicks, stat(med sd)
	
* =====================
* APPENDIX Figure D: UK 
* =====================		

	replace changes=abs(changes)
	histogram changes, by(group, title("United Kingdom") note("")) freq xtitle("Number of Changes")	name(figureD_uk, replace)	

* =====================
* APPENDIX Figure E: UK
* =====================	

	histogram time, by(group, title("United Kingdom") note("")) freq xtitle("Seconds to Complete Question") name(figureE_uk, replace)

* =====================
* APPENDIX Figure F1 
* =====================
	
	foreach i in dup libdem sdlp snp pc ukip green sf {
	set more off
	local ndup "DUP"
	local nlibdem "Liberal Democrats"
	local nsdlp "SDLP"
	local nsnp "SNP"
	local npc "Plaid Cymru"
	local nukip "UKIP"
	local ngreen "Greens"
	local nsf "Sinn Fein"
	qui sum sizepercent_`i'
		local m = r(mean)
	qui sum pollpc_`i'
		local pollm = r(mean)
	histogram sizepercent_`i', xline(`m', lpattern(solid)) xlabel(0(50)100) ///
		xline(`pollm', lpattern(tight_dot) lwidth(medthick)) freq ///
		by(group, title("`n`i''", size(medium)) note("")) ytitle("") ///
		xtitle("Percent of Seats Assigned", size(small)) name(`i', replace) nodraw
	}

	graph combine libdem green snp ukip dup sdlp pc sf, ///
		xsize(4.5) ysize(6.5) imargin(small) rows(4) note("") scale(0.9)	
	graph export "figureF1.pdf", replace
	

	
********************************************************************************
*****			  **************************************************************
*****   DENMARK   **************************************************************
*****			  **************************************************************
********************************************************************************

use "datasetDK.dta", clear

* =====================
* Table 1 DK
* =====================	
	
	/** [Row 1] Gave a 100% to one party (numzeros==9) **/	
		tab numzeros
		
	/** [Row 2] Answered the question in under 10s **/	
		tab under10s

	/** [Row 3] Answered the question in under 20s **/
		gen under20s=(time<=20) if time!=.
		tab under20s
	
	/** [Rows 4] A or B **/
		gen aorb = 0
		replace aorb = 1 if numzeros==9 | under10s==1
		tab aorb
			
	/** [Rows 5] A or C **/
		gen aorc = 0
		replace aorc = 1 if numzeros==9 | under20s==1
		tab aorc

* ==================
* Table 2 DK
* ==================	
		
	/** [Row 1] Abandoned **/	
	preserve
		* load dataset with all individuals who started the survey
		use "hazardDK.dta", clear
		quietly destring pid-q140_4, replace
			gen group=.	
			replace group = 1 if q54_1 !=.
			replace group = 2 if q52_1 !=.
			replace group = 3 if q58_1 !=.
			replace group = 4 if q56_1 !=.
			label def group 1 "seats fixed" 2 "seats random" 3 "percent fixed" 4 "percent random" 
			label val group group
		* denominators
			tabstat q54_4 q52_4 q58_4 q56_4, stats(n)
			gen reached = 1 if q54_4!=.| q52_4!=.| q58_4!=.| q56_4!=.
		* numerators: count the abandoned, by looking at missing answers for a set of questions coming after the seats question
			gen dropout=1 if reached==1
				set more off
				foreach i of varlist q105-q107 q109-q111 q113-q115 q117-q119 q121-q123 q125-q127 {
					replace dropout=0 if `i'!=.&dropout==1
				}
		* abandoned (count and percentage)
			tab dropout group, col 
	restore

	/** [Row 2] Gave a 100% to one party (numzeros==9) **/	
		tab numzeros group

	/** [Row 3] Under 10s **/
		tab under10s group, col
		
* ==================
* Figure 5 
* ==================	

	/** Social Democrats **/
		histogram sizepercent_socdem, xline(25.07, lpattern(solid)) freq ///
		by(group, title("Social Democrats") note("")) xtitle("Percent of Seats Assigned", size(small)) name(sd, replace)
	  
	/** Venstre **/
		histogram sizepercent_ven, xline(20.71, lpattern(solid)) freq /// 
		by(group, title("Venstre") note("")) xtitle("Percent of Seats Assigned", size(small)) ytitle ("") name(ven, replace)

		graph combine sd ven, xsize(8) ysize(3) scale(1.4)
		graph export "figure5.pdf", replace
		
* =====================
* APPENDIX Figure C: DK 
* =====================	

	graph box clicks if clicks<100, over(numzeros) ytitle("Number of Clicks") ///
	b1title("Number of Zeros Reported") title("Denmark") name(figureC_dk, replace)
	
	tabstat clicks, stat(med sd)

	graph combine figureC_uk figureC_dk, xsize(4) ysize(1.5) scale(1.3)
	graph export "figureC.pdf", replace
	
* =====================
* APPENDIX Figure D: DK 
* =====================		

	replace changes=abs(changes) 
		
	/** limit x-axis: make it comparable to uk graph **/
	histogram changes if changes <90, by(group, title("Denmark") note("")) ///
		xlabel(0(20)80) freq xtitle("Number of Changes") name(figureD_dk, replace)

	graph combine figureD_uk figureD_dk, xsize(4) ysize(1.5) scale(1.5)
	graph export "figureD.pdf", replace
	
* =====================
* APPENDIX Figure E: DK
* =====================	
	
	/** limit x-axis **/
	histogram time if time<3000, by(group, title("Denmark") note(""))  freq xtitle("Seconds to Complete Question") name(figureE_dk, replace)
	
	graph combine figureE_uk figureE_dk, xsize(4) ysize(1.5) scale(1.5)
	graph export "figureE.pdf", replace
	
* =====================
* APPENDIX Figure F2
* =====================	
			
	foreach i in socdem ven radven konfol socfol liball kridem danfol enh alt {
	set more off
	local nsocdem "Social Democrats"
	local nven "Venstre"
	local nradven "Radical Venstre"
	local nkonfol "Conservative People's Party"
	local nsocfol "Social People's Party"
	local nliball "Liberal Alliance"
	local nkridem "Christian Democrats"
	local ndanfol "Danish People's Party"
	local nenh "Red-Green alliance"
	local nalt "Alternative"
	qui sum sizepercent_`i'
		local m = r(mean)
	histogram sizepercent_`i', xline(`m', lpattern(solid)) xlabel(0(50)100) freq ///
		by(group, title("`n`i''", size(medium)) note("")) ytitle("") ///
		xtitle("Percent of Seats Assigned", size(small)) name(`i', replace) nodraw
	}

	graph combine radven liball danfol enh socfol konfol alt kridem, ///
		xsize(4.5) ysize(6.5) imargin(small) rows(4) note("") scale(0.9)	
	graph export "figureF2.pdf", replace

	
	
*\END
